<template>
<div>
	<el-form ref="ruleForm" :rules="rules" :model="form" label-width="120px">
      <el-form-item label="角色名称" prop="roleName">
        <el-input v-model="form.roleName" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio v-model="form.status" label="1">启用</el-radio>
        <el-radio v-model="form.status" label="0">停用</el-radio>
      </el-form-item>
    </el-form>
     <div style="text-align: center;">
      <el-button type="primary" @click="onSubmit('ruleForm')">创建角色</el-button>
      <el-button @click="onCancel">关闭</el-button>
    </div>
</div>

</template>

<script>
import request from '@/utils/request'
import qs from 'qs';
import axios from 'axios'

export default {
  data() {
    return {
      form: {},
      rules: {
        UserName: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 2 到 10 个字符', trigger: 'blur' }
        ],
        RoleId: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        RoleName: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ],
      }
    }
  },
  created() {
    // this.getRoleList();
    // this.getInstitutionList();
  },
  mounted() {

  },
  methods: {
    getRoleList() {
      request({
        url: '/role/getRoleList',
        method: 'get',
        params: {}
      }).then((res) => {
        this.listLoading = true
        this.roleList = res.result;
        this.listLoading = false
      }).catch((err) => {
        this.$message(err)
      })
    },
    getInstitutionList() {
      request({
        url: '/institution/getInstitutionList',
        method: 'get',
        params: {}
      }).then((res) => {
        this.listLoading = true
        this.institutionList = res.result;
        this.listLoading = false
      }).catch((err) => {
        this.$message(err)
      })
    },
    onSubmit(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        var userId = sessionStorage.getItem("userId");
        var userName = sessionStorage.getItem("userName");
        if (valid) {
          this.buttonLoading = true;
            this.form.token = "abc123";
            this.form.userId = userId;
            this.form.uName = userName;
          axios({
            url: '/sysRole/addSysRole',
            method: 'post',
            data: qs.stringify(this.form) 
          }).then((res) => {
              this.$message(res.data.data)
              this.$emit('refresh');
              this.$emit('closeDialog', false);
          }).catch((err) => {
            this.buttonLoading = false;
            this.$message(err)
          })
        } else {
          this.$message({
            message: '表单验证失败',
            type: 'warning'
          })

          return false;
        }
      });
    },
    onCancel() {
      this.$emit('closeDialog', false);
    },
    getDate (date){
      this.form.birth = date; 
    }
  }
}
</script>

<style scoped>
.line{
  text-align: center;
}
</style>
